//
//  HomeViewController.swift
//  iPad_QQ
//
//  Created by 李善武 on 16/4/29.
//  Copyright © 2016年 李善武. All rights reserved.
//

import UIKit

class HomeViewController: LSWBaseViewController {
    // 声明属性
    private var contentView : UIView?
    private var selectedIndex : Int = 0
    
    private lazy var dock : Dock = {
        let dock = Dock()
        self.view.addSubview(dock)
        dock.autoresizingMask = .FlexibleHeight
        dock.frame.size.height = self.view.frame.size.height
        dock.backgroundColor = UIColor.redColor()
        return dock
    }()
    //private lazy
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(red: 55/255.0, green: 55/255.0, blue: 55/255.0, alpha: 1)
        // 初始化Dock
        setupDock()
        
        // 初始化tabBarItem的控制器
        setupVC()
        
        // 初始化contentView
        setupContentView()
        
        // 显示个人中心
        iconBtnClick()
    }
    
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        // 1.判断当前屏幕
        let isLandscape = view.frame.width>view.frame.height
        // 2.设置dock宽度
        let duration = coordinator.transitionDuration()
        UIView.animateWithDuration(duration) { () -> Void in
//            self.dock.frame.size.width = isLandscape ? PortraitWidth : landscapeWidth
            self.dock.rotateToLandscape(!isLandscape)
        }
    }

}

// 初始化Dock
extension HomeViewController{
    private func setupDock(){
       // 1.判断当前屏幕方向
        let isLandscape = view.frame.width > view.frame.height
        
//       // 2.计算dock的宽度
//        dock.frame.size.width = isLandscape ? landscapeWidth:PortraitWidth
        dock.rotateToLandscape(isLandscape)
        
        // 3.设置bottomMenu代理
        dock.getMenu.delegate = self
        
        // 4.设置tabBar代理
        dock.getTb.delegate = self
        
        // 5.设置按钮监听
        dock.getIb.addTarget(self, action: "iconBtnClick", forControlEvents: .TouchUpInside)
    }
}

// 实现代理方法
extension HomeViewController : menuItemDelegate{
    func menuItemBtn(menu : BottomMenu ,menuType : menuItemType){
        switch menuType{
        case .Mood :
               let moodVC = MoodViewController()
               let moodNav = UINavigationController(rootViewController: moodVC)
               // 呈现样式
               moodNav.modalPresentationStyle = .FullScreen
               // 过渡样式
               moodNav.modalTransitionStyle = .PartialCurl
               
               presentViewController(moodNav, animated: true, completion: nil)
        case .Photo :
               print("发表了图片")
            
        case .Blog :
               print("发表了评论")
            
        }
    }
}

// 设置控制器
extension HomeViewController{
    private func setupVC(){
       let vc1 = AllStatusViewController()
       packNav(vc1)
        
        let vc2 = UIViewController()
        vc2.view.backgroundColor = UIColor.yellowColor()
        packNav(vc2)
        
        let vc3 = UIViewController()
        vc3.view.backgroundColor = UIColor.orangeColor()
        packNav(vc3)
        
        let vc4 = UIViewController()
        vc4.view.backgroundColor = UIColor.darkGrayColor()
        packNav(vc4)
        
        let vc5 = UIViewController()
        vc5.view.backgroundColor = UIColor.greenColor()
        packNav(vc5)
        
        let vc6 = UIViewController()
        vc6.view.backgroundColor = UIColor.blueColor()
        packNav(vc6)
        
        let vc7 = UIViewController()
        vc7.title = "个人中心"
        vc7.view.backgroundColor = UIColor.whiteColor()
        packNav(vc7)
    }
    private func packNav(vc : UIViewController){
       let tabNav = UINavigationController(rootViewController: vc)
       addChildViewController(tabNav)
        childViewControllers.count
    }
    
    private func setupContentView(){
        let contentView = UIView()
        view.addSubview(contentView)
        
        
        // 计算frame
        contentView.frame.size.height = self.view.frame.height - 20
        contentView.frame.origin.x = PortraitWidth
        contentView.frame.origin.y = 20
        
        // 得到竖屏宽度
        let width = min(view.frame.width, view.frame.height)
        contentView.frame.size.width = width - PortraitWidth
        
        self.contentView = contentView
    }
}

// 实现tabBar点击代理方法
extension HomeViewController : TabBarDelegate{
    func tabbarDidClickItem(from: Int, to: Int) {
        // 得到上一个点击的vc
        let previousVC = childViewControllers[from]
        // 移除上一个点击的vc的view
        previousVC.view.removeFromSuperview()
        
        // 得到现在点击的vc
        let nextVC = childViewControllers[to]
        nextVC.view.frame = (contentView?.bounds)!
        self.contentView?.addSubview(nextVC.view)
        
        // 设置将要跳转到按钮为当时按钮
        selectedIndex = to
    }
}

// 事件处理
extension HomeViewController{
    @objc private func iconBtnClick(){
       tabbarDidClickItem(selectedIndex, to: childViewControllers.count - 1)
       dock.getTb.unSelected()
    }
}
